# coding:utf-8
"""
description:set up a new home，或者join existing home
"""
import os
import random

from appium.webdriver.common import mobileby

from common.Gesture_Mainpulation import gesture_mainpulation
from common.Get_Yaml import get_yaml
from common.Log import LOG
from config.GlobalParameter import TEST_Element_YAML
from pages.Dashboard_page import dashboard_page

testElements = get_yaml(os.path.join(TEST_Element_YAML, "CreateAndJoinHome.yaml"))


class Createhome_page(dashboard_page):
    by = mobileby.MobileBy()

    def func_EnterCreateHome(self):
        """
        进入create home页面
        :return:
        """
        super().click_homeicon()
        self.swipe_ImageBtnHome()  # 滑动控件
        self.Get_ImageBtnHome()[-1].click()  # 滑动image home列表，并点击+ icon
        self.click_CreateHome()  # 点击set up a new home

    def func_CreateHome(self, homeName, customRoom):
        """
        正常create home,添加一个自定义home和随机点击两个room，断言room选择总数
        :param homeName:
        :param customRoom:
        """
        self.input_HomeName(homeName)
        self.click_btnNext()
        self.click_IncreaseFloor()  # 增加一个楼层
        self.click_btnNext()
        self.click_customRoom()
        self.input_SingleInputPopup(customRoom)  # 增加一个自定义room
        self.click_btnSingleInputPopupSave()  # 保存成功自定义room，并选中
        self.swipeandclick_CreateHomeRoomRooms()  # 滑动rooms控件并随机选中两个room name

    def func_quickstart(self):
        """
        quickstart快速开始创建home
        """
        self.click_quickStart()
        self.click_confirm()

    def func_AfterNewHome_Quickstart(self, homename):
        """
        走完homename后，在floor页面点击quick start
        :param homename:
        """
        self.input_HomeName(homename)
        self.click_btnNext()
        self.click_quickStart()
        self.click_confirm()

    def func_verifyHomename(self, homename, message):
        """

        :param homename:
        :param message:
        :return:
        """
        self.input_HomeName(homename)
        self.click_btnNext()
        return self.find_toast(message)

    def func_JoinHome_galley(self, message, x, y):
        """
        选择gallery中的home QR code，1.未添加Home QR code,图片不包含QR code
            2.扫描成功 。3.重复扫描未分权的home QR code.
            4.通过审核，已扫描添加成功home
        :param message:
        :param x: 第几个图片的x值
        :param y:第几个图片的y值
        :return:
        """
        if x == 1:  # 第一个用例执行才需要下面三个步骤
            super().click_homeicon()
            self.swipe_ImageBtnHome()  # 滑动控件
            self.Get_ImageBtnHome()[-1].click()  # 滑动image home列表，并点击+ icon
            self.click_JoinHome()  # 点击Join an existing Home
        if self.assertVisible(*self.allow_button_loc):
            self.click_allow_button()  # 点击申请访问gallery权限弹框的ALLOW按钮
        self.click_btnScanQRToGallery()
        # 选择特定图片的元素操作。对于不同手机无法实现
        self.click_Gallery()
        self.click_PhotoList(x, y)
        return self.find_toast(message)

    HeaderHome_loc = (by.ID, testElements.get_elementInfo(0))
    ImageBtnHome_loc = (by.XPATH, testElements.get_elementInfo(1))
    CreateHome_loc = (by.ID, testElements.get_elementInfo(2))

    message_loc = (by.ID, testElements.get_elementInfo(21))

    def swipe_ImageBtnHome(self):
        """滑动顶部home列表控件"""
        gesture_mainpulation().swipe_left(self.driver, self.find_element(*self.HeaderHome_loc))

    def Get_ImageBtnHome(self):
        """
        获取Home列表项
        :return:
        """
        return self.find_elements(*self.ImageBtnHome_loc)

    def Get_message(self):
        """弹框的message"""
        return self.find_element(*self.message_loc).text

    def func_SelectHome(self, HomeName):
        """
        如果是为当前home，直接点击home icon进行后续操作，
        如果不是，则依次点击 home确认
        :param HomeName:
        :return:
        """
        if super().get_homename() == "DEFAULT HOME":
            LOG.info("Dashboard页面的Home:{} 和参数:{}一致，直接进行后续测试操作。。。".format(super().get_homename(), HomeName))
            super().click_homeicon()  # 直接点击home
        else:
            LOG.info("Dashboard页面的Home和参数home name不一致，将逐一判断home 列表项。。。")
            super().click_homeicon()  # 直接点击home
            for i in range(len(self.Get_ImageBtnHome())):
                self.Get_ImageBtnHome()[i + 1].click()
                print(self.Get_message())
                if HomeName in self.Get_message():
                    super().click_confirm()
                    super().click_homeicon()  # 切换成功后，重新点击home icon元素，才能对应add floor（）
                    return
                else:
                    super().click_cancel()
                    self.swipe_ImageBtnHome()

    def click_CreateHome(self):
        """点击setup a new home"""
        self.find_element(*self.CreateHome_loc).click()

    def assert_CreateHome(self):
        """
        断言setup a new home弹框选项存在
        :return:
        """
        return self.assertVisible(*self.CreateHome_loc)

    btnNext_loc = (by.ID, testElements.get_elementInfo(3))
    quickStart_loc = (by.ID, testElements.get_elementInfo(4))

    def click_btnNext(self):
        """
        #create home过程中的按钮
        :return:
        """
        self.find_element(*self.btnNext_loc).click()

    def click_quickStart(self):
        """点击quick start 快速开始创建"""
        self.find_element(*self.quickStart_loc).click()

    """
    set up home name page
    """
    etCreateHomeHomeName_loc = (by.ID, testElements.get_elementInfo(5))

    def input_HomeName(self, CreateHomeHomeName):
        """输入home name"""
        self.send_keys(CreateHomeHomeName, self.find_element(*self.etCreateHomeHomeName_loc))

    """floor page"""
    decrease_loc = (by.ID, testElements.get_elementInfo(6))
    increase_loc = (by.ID, testElements.get_elementInfo(7))

    def click_DecreaseFloor(self):
        """减少楼层数"""
        self.find_element(*self.decrease_loc).click()

    def click_IncreaseFloor(self):
        """增加楼层数"""
        self.find_element(*self.increase_loc).click()

    """select room page"""
    FloorPrevious_loc = (by.ID, testElements.get_elementInfo(8))
    FloorNext_loc = (by.ID, testElements.get_elementInfo(9))

    """添加自定义room"""
    btnClosePopup_loc = (by.ID, testElements.get_elementInfo(10))
    etSingleInputPopup_loc = (by.ID, testElements.get_elementInfo(11))
    btnSingleInputPopupSave_loc = (by.ID, testElements.get_elementInfo(12))

    CreateHomeRoomRooms_loc = (by.ID, testElements.get_elementInfo(13))  # rooms控件
    rlRoomMgmtRoom_loc = (by.ID, testElements.get_elementInfo(14))  # room name按钮
    CreateHomeRoomRoomNum_loc = (by.ID, testElements.get_elementInfo(15))  # 已选的room数量
    CreateHomeRoomFloor_loc = (by.ID, testElements.get_elementInfo(16))  # floor层数

    def click_FloorPrevious(self):
        """点击查看上一个楼层room数"""
        self.find_element(*self.FloorPrevious_loc).click()

    def click_FloorNext(self):
        """点击查看下一个楼层room数"""
        self.find_element(*self.FloorNext_loc).click()

    def click_customRoom(self):
        """点击第一个custom，添加自定义room"""
        RoomNames = self.find_elements(*self.rlRoomMgmtRoom_loc)
        RoomNames[0].click()

    def click_btnClosePopup(self):
        """点击自定义room的弹框x按钮"""
        self.find_element(*self.btnClosePopup_loc).click()

    def input_SingleInputPopup(self, SingleInputPopup):
        """输入自定义room弹框内容"""
        self.send_keys(SingleInputPopup, self.find_element(*self.etSingleInputPopup_loc))

    def click_btnSingleInputPopupSave(self):
        """点击自定义room的弹框confirm"""
        self.find_element(*self.btnSingleInputPopupSave_loc).click()

    def swipeandclick_CreateHomeRoomRooms(self):
        """
        随机点击两个room
        :return:
        """
        SelectRoomNames = self.find_elements(*self.rlRoomMgmtRoom_loc)
        del SelectRoomNames[0]
        del SelectRoomNames[1]
        random.choice(SelectRoomNames).click()

        """获取rooms控件进行滑动"""
        gesture_mainpulation().swipe_up(self.driver, self.find_element(*self.CreateHomeRoomRooms_loc))

        random.choice(self.find_elements(*self.rlRoomMgmtRoom_loc)).click()

    def gettext_CreateHomeRoomRoomNum(self):
        """获取已选择的room数量"""
        return self.find_element(*self.CreateHomeRoomRoomNum_loc).text

    def gettext_CreateHomeRoomFloor(self):
        """获取floor名称"""
        return self.find_element(*self.CreateHomeRoomFloor_loc).text

    """点击Join an existing Home"""
    JoinHome_loc = (by.ID, testElements.get_elementInfo(17))
    """点击scan QR code page右下角的图库按钮"""
    btnScanQR_loc = (by.ID, testElements.get_elementInfo(18))

    def click_JoinHome(self):
        """点击Join an existing Home"""
        self.find_element(*self.JoinHome_loc).click()

    def click_btnScanQRToGallery(self):
        """点击scan QR code page右下角的图库按钮"""
        self.find_element(*self.btnScanQR_loc).click()

    """LG手机,点击图库跳转相片列表"""
    Gallery_loc = (by.ID, testElements.get_elementInfo(19))

    def click_Gallery(self):
        """LG手机,点击图库跳转相片列表"""
        self.find_element(*self.Gallery_loc).click()

    def click_PhotoList(self, Numx, Numy):
        """
        LG手机,点击相片列表
        :param Numx: 第几个图片的x值
        :param Numy: 第几个图片的y值
        :return:
        """
        Maxinumxy = gesture_mainpulation().get_sizes(self.driver)
        x = Maxinumxy[0] / 3  # 根据LG手机3列五行分割，取图片集的一个
        y = Maxinumxy[1] / 5  # 根据LG手机3列五行分割，取图片集的一个
        print("LG手机,点击相片列表,x----{}y----{}".format(x * Numx - 150, y * Numy))
        self.driver.tap([(x * Numx - 150, y * Numy)])

    """LG手机,点击申请访问gallery权限弹框的ALLOW按钮"""
    allow_button_loc = (by.ID, testElements.get_elementInfo(20))

    def click_allow_button(self):
        """LG手机,点击申请访问gallery权限弹框的ALLOW按钮"""
        self.find_element(*self.allow_button_loc).click()
